package com.nanjia.boot.warehouse.storagelocation.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.core.security.util.SecurityUtils;
import com.nanjia.boot.warehouse.storagelocation.converter.WarehouseStorageLocationConverter;
import com.nanjia.boot.warehouse.storagelocation.mapper.WarehouseStorageLocationMapper;
import com.nanjia.boot.warehouse.storagelocation.model.entity.WarehouseStorageLocation;
import com.nanjia.boot.warehouse.storagelocation.model.form.WarehouseStorageLocationForm;
import com.nanjia.boot.warehouse.storagelocation.model.query.WarehouseStorageLocationQuery;
import com.nanjia.boot.warehouse.storagelocation.model.vo.WarehouseStorageLocationVO;
import com.nanjia.boot.warehouse.storagelocation.service.WarehouseStorageLocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库库位服务实现类
 *
 * @author 周洋
 * @since 2025-08-22 09:39
 */
@Service
@RequiredArgsConstructor
public class WarehouseStorageLocationServiceImpl extends ServiceImpl<WarehouseStorageLocationMapper, WarehouseStorageLocation> implements WarehouseStorageLocationService {

    private final WarehouseStorageLocationConverter warehouseStorageLocationConverter;

    /**
     * 获取仓库库位分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<WarehouseStorageLocationVO>} 仓库库位分页列表
     */
    @Override
    public IPage<WarehouseStorageLocationVO> getWarehouseStorageLocationPage(WarehouseStorageLocationQuery queryParams) {
        Page<WarehouseStorageLocationVO> pageVO = this.baseMapper.getWarehouseStorageLocationPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取仓库库位表单数据
     *
     * @param id 仓库库位ID
     * @return 仓库库位表单数据
     */
    @Override
    public WarehouseStorageLocationForm getWarehouseStorageLocationFormData(Long id) {
        WarehouseStorageLocation entity = this.getById(id);
        return warehouseStorageLocationConverter.toForm(entity);
    }

    /**
     * 新增仓库库位
     *
     * @param formData 仓库库位表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveWarehouseStorageLocation(WarehouseStorageLocationForm formData, String ipAddr) {
        WarehouseStorageLocation entity = warehouseStorageLocationConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());

        return this.save(entity);
    }

    /**
     * 更新仓库库位
     *
     * @param id       仓库库位ID
     * @param formData 仓库库位表单对象
     * @param ipAddr
     * @return 是否修改成功
     */
    @Override
    public boolean updateWarehouseStorageLocation(Long id, WarehouseStorageLocationForm formData, String ipAddr) {
        WarehouseStorageLocation entity = warehouseStorageLocationConverter.toEntity(formData);
        entity.setId(id);
        entity.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
        entity.setUpdateIp(ipAddr);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 删除仓库库位
     *
     * @param ids 仓库库位ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteWarehouseStorageLocations(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的仓库库位数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public Result<List<WarehouseStorageLocationVO>> getWarehouseStorageLocationOptions(WarehouseStorageLocationQuery queryParams) {
        List<WarehouseStorageLocation> list = this.list(new LambdaQueryWrapper<WarehouseStorageLocation>().eq(queryParams.getWarehouseId() != null, WarehouseStorageLocation::getWarehouseId, queryParams.getWarehouseId()));

        return Result.success(list.stream().map(warehouseStorageLocationConverter::toVO).collect(Collectors.toList()));
    }
}
